package cn.dong.edu.controller.admin;


import cn.dong.api.domain.CommonResult;
import cn.dong.edu.domain.Course;
import cn.dong.edu.domain.dto.CourseInfo;
import cn.dong.edu.domain.dto.CourseVo;
import cn.dong.edu.domain.query.QueryCourse;
import cn.dong.edu.service.impl.CourseServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author You-Dong
 * @since 2021-02-06
 */
@RestController
@RequestMapping("/edu/course")
public class CourseController {

    @Autowired
    private CourseServiceImpl courseService;

    /**
     * 发布课程
     *
     * @param info
     * @return
     */
    @PostMapping("/create")
    public CommonResult<String> createCourse(@RequestBody CourseInfo info) {
        String id = courseService.createCourse(info);
        if (id == null) {
            return CommonResult.error();
        }
        return CommonResult.ok().data(id);
    }

    /**
     * 条件查询全部
     * @return
     */
    @GetMapping("/findAll")
    public CommonResult findAll(){
        return CommonResult.ok().data(courseService.list());
    }


    /**
     * 分页查询组合条件查询讲师
     *
     * @param current
     * @param size
     * @return
     */
    @PostMapping("/findByCondition/{current}/{size}")
    public CommonResult findByCondition(@PathVariable("current") Integer current,
                                        @PathVariable("size") Integer size,
                                        @RequestBody(required = false) QueryCourse queryTeacher) {
        IPage<Course> page = courseService.findByCondition(current, size, queryTeacher);
        return CommonResult.ok().data(page);
    }

    /**
     * 提交审显示的确认信息，详细信息
     *
     * @param courseId
     * @return
     */
    @GetMapping("/findDetailedInfoById/{courseId}")
    public CommonResult findDetailedInfoById(@PathVariable String courseId) {
        CourseVo result = courseService.findDetailedInfoById(courseId);
        return CommonResult.ok().data(result);
    }

    /**
     * 发布课程
     *
     * @param id
     * @return
     */
    @PutMapping("/publishCourse/{id}")
    public CommonResult publishCourse(@PathVariable String id) {
        QueryWrapper<Course> wrapper = new QueryWrapper<Course>();
        Course course = new Course();
        course.setStatus("Normal");
        wrapper.eq("id", id);
        return CommonResult.whether(courseService.update(course, wrapper));
    }

    /**
     * 修改课程
     *
     * @param info
     * @return
     */
    @PutMapping("/update")
    public CommonResult<String> updateCourse(@RequestBody CourseInfo info) {
        String id = courseService.updateCourse(info);
        if (id == null) {
            return CommonResult.error();
        }
        return CommonResult.ok().data(id);
    }

    /**
     * 根据id查询课程
     *
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public CommonResult findById(@PathVariable("id") String id) {
        CourseInfo result = courseService.findById(id);
        if (result == null) {
            return CommonResult.error();
        }
        return CommonResult.ok().data(result);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public CommonResult delete(@PathVariable("id") String id) {
        return CommonResult.whether(courseService.deleteCourse(id));
    }

    /**
     * 根据日期获取上线数量
     * @param day
     * @return
     */
    @GetMapping("/getCourseOnlineByDay/{day}")
    public CommonResult getCourseOnlineByDay(@PathVariable("day") String day){
        Integer count = courseService.getCourseOnlineByDay(day);
        return CommonResult.ok().data(count);
    }

}

